package config

import (
	"fmt"
	k8sclient "k8s_gui_client/k8s/client"
	"os"
	"time"
	utils "k8s_gui_client/utils"
	"github.com/xuri/excelize/v2"
)

//resourcequota list
func GetResourceQuotas(namespace string  ) (map[string]interface{},error){

	if namespace  !=  "" {
	   return  k8sclient.CurrentK8sApiClient.GetAPI("/api/v1/namespaces/"+namespace+"/resourcequotas")
	} else {
	   return  k8sclient.CurrentK8sApiClient.GetAPI("/api/v1/resourcequotas")
		
	}
	
	
}

func ResourceQuotaURL(namespace string,name string) string {
	return "/api/v1/namespaces/" + namespace + "/resourcequotas/"+name
}

func GetResourceQuotaYaml(namespace string,name string) (string ,error){
    return k8sclient.CurrentK8sApiClient.GetAPIAsYaml(ResourceQuotaURL(namespace,name))
}


func ExportResourceQuotaReports() error {

	resourcequotas, err := k8sclient.CurrentK8sApiClient.GetAPI("/api/v1/resourcequotas")
	if err != nil {
		return err
	}

	os.MkdirAll("d:/tmp", os.ModePerm)

	f := excelize.NewFile()
	f.SetSheetName("Sheet1", "resourcequotas")

	f.SetCellStr("resourcequotas", "A1", "name")
	f.SetCellStr("resourcequotas", "B1", "namespace")
	f.SetCellStr("resourcequotas", "C1", "cpu.quota")
	f.SetCellStr("resourcequotas", "D1", "cpu.used")
	f.SetCellStr("resourcequotas", "E1", "limits.cpu.quota")
	f.SetCellStr("resourcequotas", "F1", "limits.cpu.used")

	f.SetCellStr("resourcequotas", "G1", "memory.quota")
	f.SetCellStr("resourcequotas", "H1", "memory.used")
	f.SetCellStr("resourcequotas", "I1", "limits.memory.quota")
	f.SetCellStr("resourcequotas", "J1", "limits.memory.used")

	f.SetCellStr("resourcequotas", "K1", "configmaps.quota")
	f.SetCellStr("resourcequotas", "L1", "configmaps.used")

	f.SetCellStr("resourcequotas", "M1", "secrets.quota")
	f.SetCellStr("resourcequotas", "N1", "secrets.used")


	items := resourcequotas["items"].([]interface{})

	for index, item := range items {
		pod := item.(map[string]interface{})

		metadata := pod["metadata"].(map[string]interface{})

		name := metadata["name"].(string)
		namespace := metadata["namespace"].(string)

		status := pod["status"].(map[string]interface{})

		hard := status["hard"].(map[string]interface{})

		used := status["used"].(map[string]interface{})


		f.SetCellStr("resourcequotas", fmt.Sprintf("A%d", index+2), name)
		f.SetCellStr("resourcequotas", fmt.Sprintf("B%d", index+2), namespace)


		f.SetCellFloat("resourcequotas", fmt.Sprintf("C%d", index+2), utils.To_cpu_h(hard["cpu"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("D%d", index+2), utils.To_cpu_h(used["cpu"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("E%d", index+2), utils.To_cpu_h(hard["limits.cpu"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("F%d", index+2), utils.To_cpu_h(used["limits.cpu"].(string)),2,32)
	
		f.SetCellFloat("resourcequotas", fmt.Sprintf("G%d", index+2), utils.To_memory_g(hard["memory"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("H%d", index+2), utils.To_memory_g(used["memory"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("I%d", index+2), utils.To_memory_g(hard["limits.memory"].(string)),2,32)
		f.SetCellFloat("resourcequotas", fmt.Sprintf("J%d", index+2), utils.To_memory_g(used["limits.memory"].(string)),2,32)
	
		f.SetCellStr("resourcequotas", fmt.Sprintf("K%d", index+2), hard["configmaps"].(string))
		f.SetCellStr("resourcequotas", fmt.Sprintf("L%d", index+2), used["configmaps"].(string))

		f.SetCellStr("resourcequotas", fmt.Sprintf("M%d", index+2), hard["secrets"].(string))
		f.SetCellStr("resourcequotas", fmt.Sprintf("N%d", index+2), used["secrets"].(string))

	}

	filename := fmt.Sprintf("d:/tmp/resourcequotas-%s.xlsx", time.Now().Format("20060102150405000"))
	if err := f.SaveAs(filename); err != nil {
		return err
	}
	return utils.ExecRun(fmt.Sprintf("cmd.exe /k start %s ", filename))

}
